<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Index  extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('index_model');
        $this->load->model('category_model');
        $this->load->model('array_model');
        $this->load->model('article_model');

        $this->load->model('ziying_model');

        $this->load->model('comment_model');


    }

    public function index()
    {
        if(!empty($_GET['cityId']))
        {
            $city = $_GET['cityId'];
        }else{//城市默认在巴黎
            $city=1;
        }
        $newlinelist = $this->article_model->get_track_and_act(array('sortkey' =>'created','limit'=>5,'cityId'=>$city));
        $poplinelist = $this->article_model->get_track_and_act(array('sortkey' =>'zan','limit'=>3,'cityId'=>$city));

        $ziying = $this->ziying_model->getList(array('cityId'=>$city,'limit'=>3));
        $banner= $this->getbanner($city);
        if(empty($banner)) $banner='';
		if(empty($newlinelist)) $newlinelist='';
		if(empty($poplinelist)) $poplinelist='';
		if(empty($ziying)) $ziying='';
        $this->success(array('banner'=>$banner,'newlinelist'=>$newlinelist,'poplinelist'=>$poplinelist,'ziying'=>$ziying));
    }

	private function getbanner($cityId)
	{
	    $this->db->select('b.iconUrl,c.cityName');
		$this->db->from('app_banner_list as b');
		$this->db->join('mod_city as c', 'b.cityId = c.id', 'left');
		if(!empty($cityId)){
			$this->db->where('cityId',$cityId);
		}
		//默认城市不限的时候定位在巴黎
		else
		{$this->db->where('cityId',1);}
		$query = $this->db->get()->result_array();
		$banner=array();
		if(!empty($query))
		{
			foreach ($query as $q)
			{
				if(!isset($banner['cityEn'])||!isset($banner['cityCn']))
				{
				  $q['cityName']=trim($q['cityName']);
				  $q['cityName']=rtrim($q['cityName'],'|');
				  $c=explode('|', $q['cityName']);
				  $banner['cityCn']=trim($c[0]);
				  $banner['cityEn']=trim($c[1]);
				}	
				$banner['iconUrl'][]=$this->img($q['iconUrl']);
			}
		}
		$banner=array_merge($banner,$this->article_model->current_weather($banner['cityEn']));
		return $banner;
	}

    public function city()
    {
        //城市 国家 自定义 地区
        $arrays = $this->array_model->getArrays();
        if(!empty($arrays))
        {

            $this->success($arrays);
        }else{

            $this->error('未找到数据');
        }
    }
    
    public function detail()
    {
        $id = $_GET['id'];
        $data = $this->article_model->detail($id);

        $con_data['type'] = 'TRACK';
        $con_data['id'] = $id;
        $con_data['limit'] = 5;
        $con_data['offset'] = 0;
        $coms = $this->comment_model->getActivityOrTrackComments($con_data);

        $this->success(array('data'=>$data,'coms' =>$coms));

    }

}
